package com.uetoken.cn.getui;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.uetoken.cn.MainActivity;
import com.uetoken.cn.R;
import com.uetoken.cn.activity.SplashActivity;
import com.uetoken.cn.base.MyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @author 谢杨
 * @date 2018年8月7日10:47:36
 * 从2.9.5.0版本开始，为了解决小概率发生的Android广播丢失问题，我们推荐应用开发者使用新的IntentService方式来接收推送服务事件（包括CID获取通知、透传消息通知等）
 */
public class GeTuiReceiverIntentService extends GTIntentService {

    @Override
    public void onReceiveServicePid(Context context, int i) {

    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData==  clientid = " + clientid);

        Handler handler = MyApplication.getInstance().getHandler();
        handler.obtainMessage(1, clientid).sendToTarget();
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {
        String appid = gtTransmitMessage.getAppid();
        String taskid = gtTransmitMessage.getTaskId();
        String messageid = gtTransmitMessage.getMessageId();
        byte[] payload = gtTransmitMessage.getPayload();
        String pkg = gtTransmitMessage.getPkgName();
        String cid = gtTransmitMessage.getClientId();
        String payloadId = gtTransmitMessage.getPayloadId();

        // 第三方回执调用接口，actionid范围为90000-90999，可根据业务场景执行
        boolean result = PushManager.getInstance().sendFeedbackMessage(context, taskid, messageid, 90001);
//        PushManager.getInstance().

        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData== 1 result = " + result);
        String data;
        if (payload == null) {
            data = "";
        } else {
            data = new String(payload);
        }
        sendMessage(data, 101);

        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData== 1 payload1 = " + data);
        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData== -> " + "appid = " + appid + "\ntaskid = " + taskid + "\nmessageid = " + messageid + "\npkg = " + pkg
                + "\ncid = " + cid + "\npayloadId = " + payloadId);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b) {

    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {
        String data = gtNotificationMessage.getContent();
        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData== 2 payload = " + data);
    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage) {
        String data = gtNotificationMessage.getContent();
        LogUtils.dTag("onReceiveMessageData==", "onReceiveMessageData== 3 payload = " + data);


    }

    private void sendMessage(String data, int what) {
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = data;
        MyApplication.sendMessage(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
